﻿using System;
using System.Collections.Generic;
using LNBMS.Entity;
using LNBMS.BLL;
using System.Collections;
using DotNet.Utilities;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace LNBMS.Web
{
    public partial class ResetPsw : System.Web.UI.Page
    {
        static int flag;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <returns></returns>
        private object BindGrid()
        {
            string username = Session["username"].ToString();
            var cityList = new List<City>();
            var countyList = new List<County>();
            var townList = new List<Town>();
            var villageList = new List<Village>();
            if (username == "admin")
            {
                var citybll = new CityBLL();
                cityList = citybll.GetListBySql("select \"City\".cityid,\"CityInfo\".shi_name,\"City\".password from \"CityInfo\",\"City\"where \"CityInfo\".shi_id=\"City\".cityid");
            }
            else
            {
                switch (username.Length)
                {
                    case 4:
                        var countybll = new CountyBLL();
                        countyList = countybll.GetListBySql(string.Format("select \"County\".countyid,\"CountyInfo\".xian_name,\"County\".password from \"CountyInfo\",\"County\"where \"CountyInfo\".xian_id like '{0}%' and \"CountyInfo\".xian_id=\"County\".countyid", username));
                        break;
                    case 6:
                        var townbll = new TownBLL();
                        townList = townbll.GetListBySql(string.Format("select \"Town\".townid,\"TownInfo\".zhen_name,\"Town\".password from \"TownInfo\",\"Town\" where \"TownInfo\".xian_id='{0}' and (\"TownInfo\".xian_id  || \"TownInfo\".zhen_id ) =\"Town\".townid", username));
                        break;
                    case 9:
                        var villagebll = new VillageBLL();
                        villageList = villagebll.GetListBySql(string.Format("select \"Village\".villageid,\"VillageInfo\".cun_name,\"Village\".password from \"VillageInfo\",\"Village\" where \"VillageInfo\".xian_id = '{0}' and \"VillageInfo\".zhen_id ='{1}' and (\"VillageInfo\".xian_id || \"VillageInfo\".zhen_id  || \"VillageInfo\".cun_id) = \"Village\".villageid", username.Substring(0, 6), username.Substring(6, 3)));
                        break;
                }
            }

            if (cityList.Count != 0)
            {
                flag = 1;
                Grid1.DataSource = cityList;
            }
            if (countyList.Count != 0)
            {
                Grid1.DataSource = countyList;
                flag = 2;
            }
            if (townList.Count != 0)
            {
                Grid1.DataSource = townList;
                flag = 3;
            }
            if (villageList.Count != 0)
            {
                Grid1.DataSource = villageList;
                flag = 4;
            }
            Grid1.DataBind();
            return Grid1.DataSource;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Reset_Click(object sender, EventArgs e)
        {
            PasswordGenerator pg = new PasswordGenerator();
            if (flag == 1)
            {
                List<City> list = (List<City>)BindGrid();
                foreach (City l in list)
                {
                    var citybll = new CityBLL();
                    l.password = pg.Generate();
                    citybll.Update(l);
                }
            }
            else if (flag == 2)
            {
                List<County> list = (List<County>)BindGrid();
                foreach (County l in list)
                {
                    var countybll = new CountyBLL();
                    l.password = pg.Generate();
                    countybll.Update(l);
                }
            }
            else if (flag == 3)
            {
                List<Town> list = (List<Town>)BindGrid();
                foreach (Town l in list)
                {
                    var townbll = new TownBLL();
                    l.password = pg.Generate();
                    townbll.Update(l);
                }
            }
            else if (flag == 4)
            {
                List<Village> list = (List<Village>)BindGrid();
                foreach (Village l in list)
                {
                    var villagebll = new VillageBLL();
                    l.password = pg.Generate();
                    villagebll.Update(l);
                }
            }
            Server.Transfer("ResetPsw.aspx");
        }
        /// <summary>
        /// 导出到excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Export_Click(object sender, EventArgs e)
        {
            HSSFWorkbook wk = new HSSFWorkbook();
            ISheet sheet = wk.CreateSheet("密码管理");
            IRow row;
            ICell cell;
            row = sheet.CreateRow(0);
            cell = row.CreateCell(0);
            cell.SetCellValue("编号");
            cell = row.CreateCell(1);
            cell.SetCellValue("地区名");
            cell = row.CreateCell(2);
            cell.SetCellValue("密码");
            if (flag == 1)
            {
                List<City> list = (List<City>)BindGrid();
                for (int i = 0; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i+1);
                    cell = row.CreateCell(0);
                    cell.SetCellValue(list[i].id);
                    cell = row.CreateCell(1);
                    cell.SetCellValue(list[i].name);
                    cell = row.CreateCell(2);
                    cell.SetCellValue(list[i].password);
                }
            }
            else if (flag == 2)
            {
                List<County> list = (List<County>)BindGrid();
                for (int i = 0; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i+1);
                    cell = row.CreateCell(0);
                    cell.SetCellValue(list[i].id);
                    cell = row.CreateCell(1);
                    cell.SetCellValue(list[i].name);
                    cell = row.CreateCell(2);
                    cell.SetCellValue(list[i].password);
                }
            }
            else if (flag == 3)
            {
                List<Town> list = (List<Town>)BindGrid();
                for (int i = 0; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i+1);
                    cell = row.CreateCell(0);
                    cell.SetCellValue(list[i].id);
                    cell = row.CreateCell(1);
                    cell.SetCellValue(list[i].name);
                    cell = row.CreateCell(2);
                    cell.SetCellValue(list[i].password);
                }
            }
            else if (flag == 4)
            {
                List<Village> list = (List<Village>)BindGrid();
                for (int i = 0; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i+1);
                    cell = row.CreateCell(0);
                    cell.SetCellValue(list[i].id);
                    cell = row.CreateCell(1);
                    cell.SetCellValue(list[i].name);
                    cell = row.CreateCell(2);
                    cell.SetCellValue(list[i].password);
                }
            }
            using (FileStream fs = File.OpenWrite("C:\\ExportXls.xls"))
            {
                wk.Write(fs);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }
    }
}